package com.jietuo.class03;

import java.util.Arrays;


/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2024/3/3
 * @description: 荷兰国旗问题
 * 给定一个数组arr，和一个整数num。请把小于num的数放在数组的左边，等于num的数放在中间，大于num的数放在数组的右边。
 */
public class Code03_NetherlandsProblem {

    public static void main(String[] args) {
        int[] arr = new int[]{1, 3, 5, 6, 3, 4, 6, 8, 5};
//        quickSort1(arr);

//        quickSort2(arr);
        quickSort3(arr);
        System.out.println(Arrays.toString(arr));
    }

    private static void quickSort3(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process3(arr, 0, arr.length - 1);
    }

    private static void process3(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        swapArr(arr, right, left + (int) (Math.random() * (right - left + 1)));
        int[] areaEqual = netherlandsProblem(arr, left, right);
        process3(arr, left, areaEqual[0] - 1);
        process3(arr, areaEqual[1] + 1, right);
    }

    private static void quickSort1(int[] arr) {
        // 先找到一个中间位子，将小于某个数的移动到左边，大于某个数的移动到右边
        if (arr == null || arr.length < 2) {
            return;
        }
        process1(arr, 0, arr.length - 1);
    }

    private static void process1(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int mid = partition(arr, left, right);
        process1(arr, 0, mid - 1);
        process1(arr, mid + 1, right);
    }

    // 找到中间的位子
    private static int partition(int[] arr, int left, int right) {
        if (left > right) {
            return -1;
        }
        if (left == right) {
            return left;
        }
        int leftIndex = left - 1;
        int index = left;
        while (index < right) {
            if (arr[index] <= arr[right]) {
                swapArr(arr, ++leftIndex, index++);
            } else {
                index++;
            }
        }
        swapArr(arr, ++leftIndex, right);
        return leftIndex;
    }


    private static void quickSort2(int[] arr) {
        if (arr == null || arr.length < 2) {
            return;
        }
        process2(arr, 0, arr.length - 1);
    }

    private static void process2(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int[] equalArea = netherlandsProblem(arr, left, right);
        System.out.println(Arrays.toString(equalArea));
        process2(arr, left, equalArea[0] - 1);
        process2(arr, equalArea[1] + 1, right);
    }

    private static int[] netherlandsProblem(int[] arr, int left, int right) {
        if (left > right) {
            return new int[]{-1, -1};
        }
        if (left == right) {
            return new int[]{left, right};
        }
        int leftBuf = left - 1;
        int rightBuf = right;
        int index = left;
        while (index < rightBuf) {
            if (arr[index] == arr[right]) {
                index++;
            } else if (arr[index] > arr[right]) {
                swapArr(arr, index, --rightBuf);
            } else if (arr[index] < arr[right]) {
                swapArr(arr, ++leftBuf, index++);
            }
        }
        swapArr(arr, rightBuf, right);
        return new int[]{leftBuf + 1, rightBuf};
    }

    private static void swapArr(int[] arr, int index, int rightBuf) {
        int temp = arr[index];
        arr[index] = arr[rightBuf];
        arr[rightBuf] = temp;
    }


}
